package com.fit.piris.components;

import org.apache.tapestry5.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.BindingConstants;

import com.fit.piris.entities.user.User;
import com.fit.piris.pages.Index;
import com.fit.piris.services.Authenticator;


/**
 * Layout component for pages of application piris.
 */
@Import(stylesheet = { "context:/static/style.css",
		"context:/static/ddsmoothmenu.css",
		"context:/static/ddsmoothmenu-v.css", }, library = {
		"context:/static/isss.js", "context:/static/ddsmoothmenu.js",
		"context:/static/init.js" })
public class Layout {
	@Property
	private String pageName;

	@SuppressWarnings("unused")
	@Property
	@Parameter(required = true, defaultPrefix = BindingConstants.LITERAL)
	private String pageTitle;

	@SuppressWarnings("unused")
	@Property
	@Parameter(defaultPrefix = BindingConstants.LITERAL)
	private Block sidebar;

	@Property
	@Parameter(defaultPrefix = BindingConstants.LITERAL)
	private String sidebarTitle;

	@Inject
	private ComponentResources resources;

	@Inject
	private Authenticator authenticator;

	public String getClassForPageName() {
		return resources.getPageName().equalsIgnoreCase(pageName) ? "current_page_item"
				: null;
	}

	public User getUser() {
		if (authenticator.isLoggedIn()) {
			return authenticator.getLoggedUser();
		}
		return null;
	}

	public String getPrivilegeName() {
		return authenticator.getLoggedUser().getRole().toString();
	}

	@Log
	public Object onActionFromLogout() {
		authenticator.logout();
		return Index.class;
	}


}
